package com.github.zyflzz.mycalculator.recyclerview;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.github.zyflzz.mycalculator.R;

import java.util.List;
import java.util.Stack;

/**
 * @author created by zyf on 2023/3/24
 **/
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder> {

    private Context context;
    private List<Record> recordStack;

    public RecyclerViewAdapter(Context context, List<Record> recordStack) {
        this.context = context;
        this.recordStack = recordStack;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.recycleview_item, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        holder.timeTextView.setText(recordStack.get(position).getTime());
        holder.inputTextView.setText(recordStack.get(position).getInput());
        holder.resultTextView.setText(recordStack.get(position).getResult());
    }

    @Override
    public int getItemCount() {
        return recordStack.size();
    }


    public static class MyViewHolder extends RecyclerView.ViewHolder {

        private final TextView timeTextView;

        private final TextView inputTextView;

        private final TextView resultTextView;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            this.timeTextView = itemView.findViewById(R.id.history_time);
            this.inputTextView = itemView.findViewById(R.id.history_input);
            this.resultTextView = itemView.findViewById(R.id.history_result);
        }
    }
}
